# This script produces the same variables as RES_NewCreateCensusVariables.py, but using a different method of construction (one based on the internal
# points produced by the Census Bureau.  For a detailed explanation of this method, see the working paper version of my paper, "Credit Card Redlining
# Revisited", Federal Reserve Financial and Economic Discussion Series No. 2009-39.

infile = open("new_bg_censusdata2.csv","r")

outfile = open("new_censusvars_my.csv","w")
outfiler = open("new_censusvars_radius_my.csv","w")

header = infile.readline().rstrip("\n")
headerList = header.split(",")

fipsDict = {}
for line in infile:
	line = line.rstrip("\n")
	myList = line.split(",")
	
	newDict = {}
	for i in range( len(headerList) ):
		if ( i>0 ):
			newDict[ headerList[i] ] = int( myList[i] )
			
	fips = myList[0]
	if ( int( fips[0:2] ) <= 56 ):
		fipsDict[fips] = newDict

infile.close()

(temp,rest) = header.split(",",1)
outfile.write( "FIPS," + rest + ",minfips\n" )
outfiler.write( "FIPS," + rest + ",minfips\n" )

infile = open("NewBlockGroupMatches_newBG.csv","r")

for line in infile:
	line = line.rstrip("\n")
	myList = line.split("\t")
	
	if ( int( myList[0][0:2] ) > 56 ):
		continue
	
	coords = myList.pop(0).strip()
	outfile.write( coords )  # This appears to output FIPS
	outfiler.write( coords )
	
	# First, find the block group that is the minimum distance from the bureau record
	min_dist = float(5)
	min_fips = "NA"
	for block in myList:
		(thisFips,dist) = block.split(",")
		if ( float( dist ) < min_dist ):
			min_dist = float( dist )
			min_fips = thisFips
			
	if ( (min_dist>1) or (min_fips=="NA") ):    # No matches
		for i in range( len( headerList ) ):
			if ( i > 0 ):
				outfile.write( ",-1" )
				outfiler.write( ",-1" )
		outfile.write("\n")
		outfiler.write("\n")
	else:
		# output the minimum distance result first
		for i in range( len( headerList ) ):
			if ( i> 0 ):
				outfile.write("," + str( fipsDict[min_fips][headerList[i]] ) )
		if ( min_fips=="NA" ):
			min_fips = "-1"
		outfile.write( "," + min_fips + "\n")
		
		# Now output the radius result
		holdDict = {}
		for i in range( len( headerList ) ):
			if ( i>0 ):
				holdDict[ headerList[i] ] = 0
					
		for block in myList:
			(thisFips,dist) = block.split(",")
			
			if ( float( dist )<=1 ):
				for i in range( len( headerList ) ):
					if ( i>0 ):
						thisCode = headerList[i]
						if ( ( holdDict[ thisCode ] < 0 ) or (fipsDict[thisFips][thisCode]<0) ):
							holdDict[ thisCode ] = -1
						else:
							holdDict[ thisCode ] = holdDict[ thisCode ] + fipsDict[thisFips][thisCode]
		for i in range( len( headerList ) ):
			if ( i>0 ):
				outfiler.write( "," + str( holdDict[ headerList[i] ] ) )
		outfiler.write("," + min_fips + "\n")


outfile.close()
outfiler.close()
								